<template>

  <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="100px">
    <div class="com-form-title">基础信息</div>
    <el-row>
      <el-col :span="12">
        <el-row>
          <el-col :span="12">
            <el-form-item label="调拨单号" prop="allocNumber">
              <el-input v-model="form.allocNumber" :disabled="disableForm" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="调拨时间" prop="allocTime">
              <el-date-picker v-model="form.allocTime" style="width:100%" type="date" :disabled="disableForm" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="调出单位名称" prop="recallUnitName">
              <el-input v-model="form.recallUnitName" :disabled="disableForm" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="调出单位责任人" prop="recallUnitHead">
              <el-input v-model="form.recallUnitHead" :disabled="disableForm" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-col>
      <el-col :span="12">
        <el-row>
          <el-col :span="12">
            <el-form-item label="调入单位名称" prop="transferUnitName">
              <el-input v-model="form.transferUnitName" :disabled="disableForm" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="调入单位责任人" prop="transferUnitHead">
              <el-input v-model="form.transferUnitHead" :disabled="disableForm" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" :disabled="disableForm" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-col>
      <el-col :span="12">
        <el-row>
          <el-col :span="12">
            <el-form-item label="流程状态" prop="status">
              <el-select v-model="form.status" :disabled="disableForm" style="width: 100%;" placeholder="请选择流程状态">
                <el-option v-for="item in workflowStatus" :key="item.name" :label="item.name" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="编制人" prop="founderName">
              <el-input v-model="form.founderName" :disabled="disableForm" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="编制时间" prop="createTime">
              <el-date-picker v-model="form.createTime" style="width:100%" type="date" :disabled="disableForm" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="编制人单位" prop="founderUnit">
              <el-input v-model="form.founderUnit" :disabled="disableForm" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-col>
    </el-row>

    <div style="padding-bottom: 10px;">
      <el-button type="primary" @click="showTargetSetListModal = true">引入设备</el-button>
      <el-button type="primary" @click="deleteDetail">删除设备</el-button>
    </div>
    <com-table
      v-model="selectRow"
      highlight-current-row
      :data="targetList"
      :columns="columns"
      :no-page="true"
    >
      <template v-slot:assetNumber="{row}">
        <el-input v-model="row.assetNumber" />
      </template>
      <template v-slot:devNumber="{row}">
        <el-input v-model="row.devNumber" />
      </template>
      <template v-slot:devName="{row}">
        <el-input v-model="row.devName" />
      </template>
      <template v-slot:specification="{row}">
        <el-input v-model="row.specification" />
      </template>
      <template v-slot:payTime="{row:{payTime}}">
        {{ dateFormat(payTime) }}
      </template>
      <template v-slot:remark="{row}">
        <el-input v-model="row.remark" />
      </template>

    </com-table>
    <el-button style="margin-top:10px;" type="primary" @click="showFileModal = true">附件资料({{ fileList.length }})</el-button>
    <file-uploader v-model="fileList" :show-modal.sync="showFileModal" :disabled="disableForm" :can-download="optType==='detail'" />

  </el-form>

</template>

<script>
import { addDeviceAllocationInfo, updateDeviceAllocationInfo, getDeviceAllocationInfoDetail } from '@/api/consumableManage'
import { getConsumableLedgerList } from '@/api/consumableManage'
// import { getAreaList } from '@/api/common'
import { workflowStatus } from '@/utils/dicData'
import moment from 'moment'
import fileUploader from '@/components/FileUploader'
export default {
  name: 'AddDeviceAllocationInfo',
  components: {
    fileUploader
  },
  props: {

    optType: {
      type: String,
      default: 'detail'
    },

    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      getConsumableLedgerList,
      showFileModal: false,
      selectRow: null,
      targetList: [],
      workflowStatus,

      addType: '', // same   under
      visible: false,
      showAdd: false,
      selectedNode: null,
      form: {
        allocNumber: '',
        allocTime: '',
        recallUnitName: '',
        recallUnitHead: '',
        transferUnitName: '',
        transferUnitHead: '',
        remark: '',
        addWorkflowId: '',
        status: '',
        founderName: '',
        createTime: '',
        founderUnit: ''
      },
      columns: [
        {
          label: '资产编号',
          prop: 'assetNumber'
        },
        {
          label: '设备编号',
          prop: 'devNumber'
        },
        {
          label: '设备名称',
          prop: 'devName'
        },
        {
          label: '规格',
          prop: 'specification'
        },
        {
          label: '购入时间',
          prop: 'payTime',
          slot: 'payTime',
          width: '90'
        },
        {
          label: '备注',
          prop: 'remark'
        }
      ],
      showTargetSetListModal: false,
      fileList: [
      ],
      rules: {
        name: [
          { required: true, message: '单位名称', trigger: 'change' }
        ],
        unifiedSocialCreditCode: [
          { required: true, message: '统一社会信用代码', trigger: 'change' }
        ]
      },
      provinceData: [],
      cityData: []
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    }
  },
  watch: {
    data: {
      handler(newData, oldData) {
        if (newData.id) {
          this.getDocDetail(newData.businessId)
        }
      },
      deep: true,
      immediate: true
    }
  },
  mounted() {

  },
  methods: {
    dateFormat(data) {
      if (data) {
        return moment(data).format('YYYY-MM-DD')
      } else {
        return ''
      }
    },
    getSelectedRows(data) {
      this.targetList = this.targetList.concat(data.map(item => {
        return {
          ...item
        }
      }))
    },
    getDocDetail(id) {
      getDeviceAllocationInfoDetail({ deviceAllocationInfoId: id }).then(res => {
        const { lstAttachment, deviceAllocationInfo, lstDeviceAllocationDetail } = res.data || {}
        if (lstAttachment && lstAttachment.length > 0) {
          this.fileList = lstAttachment
        }
        Object.keys(this.form).forEach(key => {
          const source = deviceAllocationInfo[key]
          if (typeof source === 'boolean') {
            this.form[key] = source ? 1 : 0
          } else {
            this.form[key] = source
          }
        })
        if (lstDeviceAllocationDetail && lstDeviceAllocationDetail.length > 0) {
          this.targetList = lstDeviceAllocationDetail
        }
      })
    },
    deleteDetail() {
    },
    saveDoc() {
      if (this.optType === 'detai') {
        this.show = false
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.targetList.map(item => {
            item.id = ''
          })
          const params = {
            deviceAllocationInfo: {
              ...this.form,
              ...this.optType === 'edit' ? {
                id: this.data.id
              } : {}
            },
            lstAttachment: this.fileList,
            lstDeviceAllocationDetail: this.targetList
          }
          const fn = this.optType === 'add' ? addDeviceAllocationInfo : updateDeviceAllocationInfo
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
              this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}

::v-deep .el-form-item__label{
  color: #000;
}
::v-deep .el-input.is-disabled .el-input__inner{
  color: #000;
}
::v-deep .el-input.is-disabled .el-input__inner{
  color: #000;

}
</style>
